package com.example.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sms.entity.Administrators;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.SimpleException;
import com.example.sms.mapper.AdministratorsMapper;
import com.example.sms.service.AdministratorsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdministratorsServiceImpl extends ServiceImpl<AdministratorsMapper , Administrators> implements AdministratorsService {
    @Autowired
    AdministratorsMapper administratorsMapper;
    @Override
    public Integer register(RegisterDTO registerDTO) {
        Administrators administrators = new Administrators();
        BeanUtils.copyProperties(registerDTO,administrators);
        return administratorsMapper.insert(administrators);
    }

    @Override
    public Administrators login(LoginDTO loginDTO) {
        if (StringUtils.isBlank(loginDTO.getUsername()))
            throw new SimpleException("用户名不能为空");
        else if (StringUtils.isBlank(loginDTO.getPassword()))
            throw new SimpleException("密码不能为空");
        Administrators administrators =
                administratorsMapper.selectOne
                        (new QueryWrapper<Administrators>()
                                .eq("username",loginDTO.getUsername())
                                .eq("password",loginDTO.getPassword()));
        if (administrators == null)
            throw new SimpleException("用户名或密码错误");
        else return administrators;
    }
}
